script_enemy_main {
	#include_function ".\lib\Common_fairy1.dnh"
	#include_function ".\se\se_index.dnh"

	let angle = GetAngle;
	let speed = GetSpeed;
	let r;

	let shot = true;

	sub initialize {
		if(arg == 1) {
			score = 5000;
			life  = 10;
			rate1 = 100;
			rate2 = 100;
		}else if(arg == 0) {
			score = 8000;
			life  = 30;
			rate1 = 50;
			rate2 = 120;
		}else {
			score = 15000;
			life  = 50;
			rate1 = 60;
			rate2 = 200;
		}
		setData;
		TMain;
	}

	sub mainLoop {
		SetCollisionA(GetX, GetY, 16);
		yield;
	}

	sub drawLoop {
	}

	sub finalize {
		if(arg == 1) {
			CreateItem(ITEM_SCORE, GetX, GetY);
		}else if(arg == 0) {
			loop(8) {
				let r = rand(0, 32);
				let angle = rand(0, 360);
				CreateItem(ITEM_SCORE, GetX +cos(angle) * r, GetY + sin(angle) * r);
			}
		}else if(arg == 2) {
			CreateItem(ITEM_BOMB, GetX, GetY);
		}
	}


/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

	task TMain {
		alternative(speed)
		case(-1) {
			r = ((GetClipMaxX - GetClipMinX)^2 + 192^2)^(1/2);
			TPosition01(GetClipMinX, GetClipMinY - 192, 1);
			SetAngle(0);
			SetSpeed(0);
			angle = atan2(GetY - (GetClipMinY - 192), GetX - GetClipMinX);

			TShot(1);
		}
		case(-2) {
			r = ((GetClipMaxX - GetClipMinX)^2 + 192^2)^(1/2);
			TPosition01(GetClipMaxX, GetClipMinY - 192, -1);
			SetAngle(0);
			SetSpeed(0);
			angle = atan2(GetY - (GetClipMinY - 192), GetX - GetClipMaxX);

			TShot(1);
		}
		case(-11) {
			let x = rand(GetClipMinX + 16, cx - 16);
			let y = rand(GetClipMinY + 32, GetClipMaxY - 128);
			setPosition(x, y);
			SetAngle(0);
			SetSpeed(0);
			Concentration01(10);
			wait(20);
			TShot(2);
			TPosition02(180);
		}
		case(-12) {
			let x = rand(GetClipMaxX - 16, cx + 16);
			let y = rand(GetClipMinY + 32, GetClipMaxY - 128);
			setPosition(x, y);
			SetAngle(0);
			SetSpeed(0);
			Concentration01(10);
			wait(20);
			TShot(2);
			TPosition02(180);
		}
		case(-13) {
			let x = cx;
			let y = GetClipMinY + 64;
			setPosition(x, y);
			SetAngle(0);
			SetSpeed(0);
			Concentration01(10);
			wait(20);
			TShot(2);
			TPosition02(180);
		}
		
		case(-3) {
			SetDamageRate(100, 100);
			SetLife(100);
			SetAngle(90);
			SetSpeed(1);
			TShot(3);
		}

		others {}
	}

	task TPosition01(_cx, _cy, dir) {
		yield;
		loop {
			angle += (0.3 * dir);
			SetX(_cx + cos(angle) * r);
			SetY(_cy + sin(angle) * r);
			yield;
		}
	}

	task TPosition02(w) {
		yield;
		wait(w);
		shot = false;
		let angle = rand(-60, -120);
		loop {
			setPosition(GetX + cos(angle) * 1.2, GetY + sin(angle) * 1.2);
			yield;
		}
	}

	task TShot(type) {
		alternative(type)
		case(1) {
			loop {
				createNmlShot01_(GetX, GetY, 3.5, GetAngleToPlayer, GRAIN, WHITE, 0);
				wait(10);
			}
		}
		case(2) {
			let color;
			if(arg == 1) {
				color = BLUE;
			}else if(arg == 0) {
				color = RED;
			}else {
				color = GREEN;
			}
			while(shot) {
				let baseAngle = GetAngleToPlayer;
				let way = 14;
				let span = 360 / way;
				let angle = baseAngle - span * (way - 1) / 2;
				loop(way) {
					createNmlShot01_(GetX, GetY, 0.8, angle, SMALL, color, 1);
					angle += span;
				}

				wait(90);
			}
		}
		case(3) {
			let way = 18;
			let span = 360 / way;
			
			let color;
			if(arg == 1) {
				color = BLUE;
			}else if(arg == 0) {
				color = RED;
			}else {
				color = GREEN;
			}
			
			loop {
				let angle = rand(0, 360);
				loop(way) {
					createNmlShot01_(GetX + cos(angle) * 8, GetY + sin(angle) * 8, 0.5, angle, RICE, color, 0);
					angle += span;
				}
				
				wait(150);
			}
		}
		others {}
	}

	#include_function ".\lib\Common_fairy2.dnh"
}